import {
    login,
    verify,
    loginOut,
    getMenuList
} from "@/services/User.js";

export default {
    namespaced: true, //打开命名空间，主要是方便mapState使用
    state: {
        data: JSON.parse(localStorage.getItem("user")) || '[]',
        // data:'[]',
        isLoading: false,
        menuList: JSON.parse(localStorage.getItem("menuList")) || '[]',
        // menuList:'[]'
    },
    mutations: {
        setIsLoading(state, payload) {
            state.isLoading = payload;
        },
        setData(state, payload) {
            state.data = payload;
        },
        setMenuList(state, payload) {
            state.menuList = payload;
        },
    },
    actions: {
        async login(context, payload) {
            var flag = false;
            context.commit("setIsLoading", true);
            var resp = await login(payload);
            if (resp.code == 0) {
                flag = true;
                context.commit("setData", resp.data);
                context.commit("setMenuList", resp.data.menus);
            }
            context.commit("setIsLoading", false);

            return flag;
        },
        async verify(context) {
            context.commit("setIsLoading", true);
            var resp = await verify();
            if (resp != null && resp.code == 0) {
                // context.commit("setData",resp.data);
                // context.commit("setMenuList",resp.data.menus);
                context.commit("setIsLoading", false);
                return true;
            } else {
                return false
            }
        },
        loginOut(context) {
            loginOut();
            context.commit("setData", null);
            context.commit("setMenuList", null);
        },
    }
}